/* This example has been placed in the public domain. */
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
#include <SDL.h>
#include "fake437.h"

int main(int argc, char *argv[]){
  SDL_Surface *screen;
  FAKE437 *fake437;
  SDL_Rect *rects;
  SDL_Event ev;
  int i;
  int j;
  int n;
  Sint16 x;
  Sint16 y;
  int done = 0;
  SDL_Color col = {72, 200, 0, 0};
  SDL_Init(SDL_INIT_VIDEO);

  screen = SDL_SetVideoMode(F437_DEFAULT_WIDTH_PX,
			    F437_DEFAULT_HEIGHT_PX, 32, 0);
  fake437 = f437_Create(screen, F437_DEFAULT_WIDTH, F437_DEFAULT_HEIGHT);

  for(j = 0 ; j < 16 ; j++)
    for(i = 0 ; i < 16 ; i++)
      f437_SetCharColor(fake437, i, j, i+j*16, i, j);
  rects = f437_GetDirtyRects(fake437, &n);
  f437_Update(fake437);
  SDL_UpdateRects(screen, n, rects);
  free(rects);

  while(!done)
    while(SDL_PollEvent(&ev)){
      if(ev.type == SDL_KEYDOWN)
	switch(ev.key.keysym.sym){
	case SDLK_ESCAPE:
	  done = 1;
	  break;
	case SDLK_TAB:
	  f437_SetCharDef(fake437, 65, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF");
	  rects = f437_GetDirtyRects(fake437, &n);
	  f437_Update(fake437);
	  SDL_UpdateRects(screen, n, rects);
	  free(rects);
	  break;
	case SDLK_UP:
	  f437_SetColorDef(fake437, 0, &col);
	  rects = f437_GetDirtyRects(fake437, &n);
	  f437_Update(fake437);
	  SDL_UpdateRects(screen, n, rects);
	  free(rects);
	  SDL_Flip(screen);
	default:
	  break;
	}
      else if(ev.type == SDL_MOUSEBUTTONDOWN){
	f437_GetCellCoords(fake437, ev.button.x, ev.button.y,
			   &x, &y);
	printf("CLICK: %d, %d (%d, %d)\n", 
	       ev.button.x, ev.button.y, x, y);
      }
    }

  f437_Destroy(fake437);
  SDL_Quit();
  return 0;
}
